﻿// ==UserScript==
// @name			d2jsp hunsub postblocker
// @namespace		http://www.mynamespace.com
// @description		d2jsp hunsub postblocker, rang adder, etc
// @include			http://forums.d2jsp.org/*
// @require			https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==

$(document).ready(function(){
	addRanks();
	makeGetBlockRequest();
	appendBlockLinks();
});

function makeRequest(data) {
	var jsonpURL = "";
	if(data.type == 0){
		jsonpURL = "http://kuzditomi.zxq.net/hunsub/?json&blocker="+data.act_user;
	}
	else if(data.type == 1){
		jsonpURL = "http://kuzditomi.zxq.net/hunsub/?json&add&blocker="+data.act_user+"&blocked="+data.blocked_user;
	}
	else if(data.type == 2){
		jsonpURL = "http://kuzditomi.zxq.net/hunsub/?json&remove&blocker="+data.act_user+"&blocked="+data.unblock_user;
	}
	GM_xmlhttpRequest({
		method: "GET",
		url: jsonpURL, // kiegészíteni szépen
		onload: function(xhr) {
			var data = eval('(' + xhr.responseText + ')');
			$.each(data,function(key,val){ // val már a jó értékek
				var toHide = $('dl dt [href="user.php?i='+val+'"]').parent().parent();
				toHide.html('<dl><dt>Tiltott felhasználó</dt><dd><center><b>blokkolt tartalom</b> <a id="'+val+'" class="unblocklink" href="javascript:void(0)">unblock</a></center></dd><dl>');
				toHide.find('a').click(function(){
					makeUnBlockRequest($(this).attr('id'));
				});
			});
		}
	});
}
function makeUnBlockRequest(unblockId){
	userURL = $("div.barL a:first").attr('href');
	userId = userURL.substring(11,userURL.length);
	data = {type:2,act_user:userId,unblock_user:unblockId};
	makeRequest(data);
	window.location.reload();
}
function makeGetBlockRequest(){
	userURL = $("div.barL a:first").attr('href');
	userId = userURL.substring(11,userURL.length);
	data = {type:0,act_user:userId};
	makeRequest(data);
}
function makeBlockRequest(sender){
	userURL = $("div.barL a:first").attr('href');
	userId = userURL.substring(11,userURL.length);
	blockUserURL=sender.parent().parent().parent().parent().find('dt a').attr('href');
	blockUserId = blockUserURL.substring(11,blockUserURL.length);
	data = {type:1,act_user:userId,blocked_user:blockUserId};
	makeRequest(data);
}
function appendBlockLinks(){
	$('div.fR.links').append('<a class="blocklink" href="javascript:void(0)">block!</a>');
	$('a.blocklink').click(function(){
		userURL = $("div.barL a:first").attr('href');
		userId = userURL.substring(11,userURL.length);
		makeBlockRequest($(this));
	});
}
function addRanks(){
	var posts = $("dl");
	posts.each(function(element){
		var content = $(this).find("td.desc.bc1").html();
		var splitat = content.indexOf("</a><br>");
		var rangstart = content.search('[0-9][0-9]*[,]?[0-9]*[ ]*<a href="pm');
		var rangend = content.search('<a href="pm');
		var postcount = parseInt(content.substring(rangstart,rangend).replace(',',''));
		// rang kiszamitasa
		if(postcount==0)
			rang = '-';
		else if(postcount <= 100)
			rang = 'Multi';
		else if(postcount <= 500)
			rang = 'Newbie';
		else if(postcount <= 1000)
			rang = 'Trainee';
		else if(postcount <= 2000)
			rang = 'Beginner';
		else if(postcount <= 5000)
			rang = 'Veteran';
		else if(postcount <= 10000)
			rang = 'Post hunter';
		else if(postcount <= 20000)
			rang = 'Spamer';
		else if(postcount <= 40000)
			rang = 'Nolifer';
		else
			rang = 'Almost beni';
		
		if($(this).find("dt a").attr('href').substr(11,6) == '266692')
			rang = 'Emperor';
		// rang hozzafuzese
		$(this).find("td.desc.bc1").append('Rank: ' + rang);
	});
}